<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="Edit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :page-sizes="[100, 200, 300, 400]"
                :page-size="100"
                layout="total, sizes, prev, pager, next, jumper"
                :total="400"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        title="编辑弹层"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoles, delRoles, addRoles, updateRoles } from '@/api/settings'
export default {
  data() {
    return {
      showDialog: false,
      // 存放弹层需要添加的数据
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      list: [], // 用于存储角色信息
      pageParams: {
        page: 1,
        pagesize: 2
      },
      total: 0
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 加载角色信息
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        console.log(res)
        this.list = res.data.rows
        console.log('list', this.list)
      } catch (error) {
        console.log(error)
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageParams.page = val
      // 重新加载页面
      this.loadRoles()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pageParams.page = val
      // 重新加载页面
      this.loadRoles()
    },
    // 删除角色信息
    async doDel(id) {
      try {
        const res = await delRoles(id)
        console.log(res)
        // 1. 提示
        this.$message.success('删除成功')
        // 删除功能bug解决
        // 如果数据删完则返回前一页
        if (this.list.length === 1) {
          this.pageParams.page--
        }
        // 删除到只剩一页
        if (this.pageParams.page <= 0) {
          this.pageParams.page = 1
        }
        // 2. 重新请求
        this.loadRoles()
      } catch (err) {
        console.log(err)
        this.$message.error('删除失败')
      }
    },
    // 用户点击删除
    hDel(id) {
      this.$confirm('是否确认删除?'
      ).then(() => {
        this.doDel(id)
      }).catch(err => err)
    },
    // 确定添加角色
    async  hSubmit() {
      const res = await addRoles(this.roleForm)
      console.log('添加角色', res)
      this.loadRoles() // 更新数据
      this.$message.success('添加角色成功')
      this.showDialog = false
    },
    // 编辑角色实现数据的修改
    async hEdit() {
      try {
        const res = await updateRoles(this.roleForm)
        this.showDialog = true
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    Edit(row) {
      this.showDialog = true
      this.roleForm = row
      this.loadRoles()
    }

  }
}
</script>
